package com.Beans.Dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.Beans.Models.Empleado;
import com.Beans.Dao.HibernateUtil;


public class EmpleadoDAO {
	
	public static Empleado findById(int id){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		Empleado e = null;
		try {
			tr = session.beginTransaction();
			Query q= session.createQuery("from Empleado e WHERE e.id = :id");
			q.setParameter("id", id);
			e = (Empleado) q.uniqueResult();
			tr.commit();
			
		}catch (HibernateException ex) {
			tr.rollback();
			ex.printStackTrace();
		}finally{
			session.close();
		}
		return e;
	}
	
	public static Empleado findByLogin(String user, String password){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		Empleado e = null;
		try {
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM Empleado e WHERE e.user = :user AND e.password = :password");
			q.setParameter("user", user);
			q.setParameter("password", password);
			e = (Empleado) q.uniqueResult();
			tr.commit();
		}catch (HibernateException ex) {
			tr.rollback();
			ex.printStackTrace();
		}finally{
			session.close();
		}
		return e;
	}
}
